import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
// 导入组件
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../component/welcome.vue'
import Users from '../component/users/users.vue'
import Rights from '../component/power/rights.vue'
import Roles from '../component/power/roles.vue'
import Cate from '../component/goods/cate.vue'
import Params from '../component/goods/params.vue'
import List from '../component/goods/list.vue'
import Add from '../component/goods/add.vue'
import Order from '../component/order/order.vue'
import Report from '../component/report/report.vue'
// 创建路由实例对象
const router = new VueRouter({
  routes:[
    {path:'/',redirect:'/login'},
    {path:'/login',component:Login},
    {
      path:'/home',
      redirect: '/welcome',
      component:Home,
      children:[{
        path:'/welcome',
        component:Welcome
      },
      {path: '/users',component:Users},
      {path: '/rights',component:Rights},
      {path: '/roles',component:Roles},
      {path:'/categories',component: Cate},
      {path:'/params',component: Params},
      {path:'/goods',component: List},
      {path:'/goods/add',component: Add},
      {path:'/orders',component: Order},
      {path:'/reports',component: Report}
      ]
    },
    

    
  ]
})
// 导航守卫：控制路由的访问权限
router.beforeEach((to,from,next)=> {
  const tokenStr=window.sessionStorage.getItem('token');
  if(to.path==='/login')return next();
  if(!tokenStr) {
    return next('/login');
  }
  next();
})
export default router
